package com.tianqu.tms.www.transfer.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jcifs.smb.SmbFile;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.VehicleBrand;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.transfer.actionform.TransferRgAdvanceSearchInputActionForm;
import com.tianqu.tms.www.transfer.controller.TransferRgAdvanceInputConfirmController;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgAdvanceInputViewHelper;

/***
 * 
 * 待办受理新增确认Action
 * 
 * @author Administrator
 * 
 */
public class TransferRgAdvanceInputConfirmAction extends TmsAction {

	private Logger logger = Logger.getLogger(TransferRgAdvanceInputConfirmAction.class);

	private String carName;

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		TransferRgAdvanceSearchInputActionForm inputForm = (TransferRgAdvanceSearchInputActionForm) form;
		TransferRgAdvanceInputViewHelper view = (TransferRgAdvanceInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);

		view = setViewFromForm(inputForm, view);
		view.setVin(inputForm.getVin());
		view.setSerialBsFlag((String) uc.getAttribute("serial"));
		String actionForward = "";

		if (inputForm.getSaveInputInfo() != null&& !"".equals(inputForm.getSaveInputInfo())) {
			actionForward = "goResultAction";
		} else {
			if (inputForm.getSearchBaseInfo() != null && !"".equals(inputForm.getSearchBaseInfo())) {
				Car car = Car.getInstance();
				car = car.findCarBaseInfoByVin(inputForm.getVin());
				if(car!=null)
				{
					if(car.getVin()!=null && !"".equals(car.getVin()))
					{
						view = setViewhelperFromEntity(view, car);
					}else{
						setErrorsToRequest(request, TmsConstants.IA003, null);
						return mapping.getInputForward();
					}				
				}else{
					setErrorsToRequest(request, TmsConstants.IA003, null);
					return mapping.getInputForward();					
				}
			}
			if ((inputForm.getGetPhoto() != null&& !"".equals(inputForm.getGetPhoto()))||
					(inputForm.getPicBtn1() != null&& !"".equals(inputForm.getPicBtn1()))||
					(inputForm.getPicBtn2() != null&& !"".equals(inputForm.getPicBtn2()))||
					(inputForm.getPicBtn3() != null&& !"".equals(inputForm.getPicBtn3()))||
					(inputForm.getPicBtn4() != null&& !"".equals(inputForm.getPicBtn4()))||
					(inputForm.getPicBtn5() != null&& !"".equals(inputForm.getPicBtn5()))||
					(inputForm.getPicBtn6() != null&& !"".equals(inputForm.getPicBtn6()))
					) {	
					   String ip = request.getHeader("x-forwarded-for");
				       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				           ip = request.getHeader("Proxy-Client-IP");
				       }
				       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				           ip = request.getHeader("WL-Proxy-Client-IP");
				       }
				       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				           ip = request.getRemoteAddr();
				       }
					String picPoint="";
					//String path = "smb://" + "192.168.2.79" + "/1/";
					String path = "smb://" + ip+ "/1/";
					SmbFile file = new SmbFile(path);
					if (!file.exists()) {
						logger.error("path不存在.");
					} else if (file.isDirectory())
					{
						SmbFile[] files = file.listFiles();
						if(files.length == 0)
						{
							setErrorsToRequest(request, TmsConstants.IM018, null);
							return mapping.getInputForward();
						}
					}
					if(inputForm.getPicBtn1() != null&& !"".equals(inputForm.getPicBtn1()))
					{
						picPoint = "1";
					}
					if(inputForm.getPicBtn2() != null&& !"".equals(inputForm.getPicBtn2()))
					{
						picPoint = "2";
					}
					if(inputForm.getPicBtn3() != null&& !"".equals(inputForm.getPicBtn3()))
					{
						picPoint = "3";
					}
					if(inputForm.getPicBtn4() != null&& !"".equals(inputForm.getPicBtn4()))
					{
						picPoint = "4";
					}
					if(inputForm.getPicBtn5() != null&& !"".equals(inputForm.getPicBtn5()))
					{
						picPoint = "5";
					}  
					if(inputForm.getPicBtn6() != null&& !"".equals(inputForm.getPicBtn6()))
					{
						picPoint = "6";
					}  					
			       				
				String sysPath = request.getSession().getServletContext().getRealPath("/").replace("/tmsweb", "");
				uc.setAttribute("serial", view.getSerial());
				uc.setAttribute("sysPath", sysPath);
				uc.setAttribute("localIp", ip);
				uc.setAttribute("picPoint", picPoint);
				TransferRgAdvanceInputConfirmController controller = new TransferRgAdvanceInputConfirmController();
				controller.execute(uc);
				TransferAcceptedInfo accept = new TransferAcceptedInfo();
				accept = accept.findTransStatusBySerial(view.getSerial());
				if (accept != null) {
					if((accept.getMaterialImages1()==null || "".equals(accept.getMaterialImages1()))&&!"6".equals(picPoint)) 
					{
						setErrorsToRequest(request, TmsConstants.IM007, null);
						return mapping.getInputForward();
					}
				} else {
					setErrorsToRequest(request, TmsConstants.IM007, null);
					return mapping.getInputForward();
				}
			}
			// 返回录入页面
			actionForward = "goCurrentlyAction";
		}
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
		accept = accept.findTransStatusBySerial(view.getSerial());
		if (accept != null) {
			view.setMaterialImages1(accept.getMaterialImages1());
			view.setMaterialImages2(accept.getMaterialImages2());
			view.setMaterialImages3(accept.getMaterialImages3());
			view.setMaterialImages4(accept.getMaterialImages4());
			view.setMaterialImages5(accept.getMaterialImages5());
			if(accept.getOwnersGroupPhoto()!=null && !"".equals(accept.getOwnersGroupPhoto())) 
			{
				view.setOwnersGroupPhoto(accept.getOwnersGroupPhoto());
			}
		}
		uc.removeAttribute(TmsConstants.VIEWHELP);
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(actionForward);
	}

	/**
	 * 将页面form信息set到viewhelp中
	 * 
	 * @throws TmsDaoException
	 */
	private TransferRgAdvanceInputViewHelper setViewFromForm(
			TransferRgAdvanceSearchInputActionForm form, TransferRgAdvanceInputViewHelper view)
			throws TmsDaoException {
		VehicleBrand brand = VehicleBrand.getInstance();
		CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
		if (StringUtil.isNullOrBlank(form.getBrandId())) {
			setCarName(" ");
		} else {
			List list = new ArrayList();
			if(form.getFromType() == 1){
				list = brand.getAllVehicleBrandInfo(form.getVendorId());
			}else{
				list = commercialVehicle.getAllVehicleBrandInfo(form.getVendorId());
			}
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					Map map = (Map) list.get(i);
					if (map.get("brandId").equals(form.getBrandId())) {
						String carName = (String) map.get("brandName");
						setCarName(carName);
					}
				}
			} else {
				setCarName(" ");
			}
		}

		view.setCarName(getCarName());
		view.setEngine(form.getEngine());
		if (form.getColor() != null && !"".equals(form.getColor())) {
			view.setColor(form.getColor());
		} else {
			view.setColor("");
		}
		view.setRegDate(form.getRegDate());
		view.setVendorCode(form.getVendorCode());
		view.setModelCode(form.getModelCode());

		if (form.getCarUse() != null && !"".equals(form.getCarUse())) {
			view.setCarUse(form.getCarUse());
		} else {
			view.setCarUse("");
		}
		if (form.getCarType() != null && !"".equals(form.getCarType())) {
			view.setCarType(form.getCarType());
		} else {
			view.setCarType("");
		}

		view.setSendCardDate(form.getSendCardDate());
		if (form.getCardType() != null && !"".equals(form.getCardType())) {
			view.setCardType(form.getCardType());
		} else {
			view.setCardType("");
		}
		view.setRegisterCardNo(form.getRegisterCardNo());
		if (form.getCountryStyle() != null
				&& !"".equals(form.getCountryStyle())) {
			view.setCountryStyle(form.getCountryStyle());
		} else {
			view.setCountryStyle("");
		}

		view.setCompanyContactPerson(form.getCompanyContactPerson());
		view.setCarOwnerIdCardNo(form.getCarOwnerIdCardNo());
		view.setOwnerOrgNo(form.getOwnerOrgNo());

		view.setOwnerType(form.getOwnerType());
		view.setOwnerPhone(form.getOwnerPhone());
		view.setOwnerAddress(form.getOwnerAddress());
		view.setOwnerOrgNo(form.getOwnerOrgNo());
		view.setCarOwner(form.getCarOwner());
		view.setCarOwnerIdCardNoOld(form.getCarOwnerIdCardNoOld());
		view.setOwnerAddressOld(form.getOwnerAddressOld());
		view.setAgentName(form.getAgentName());
		view.setAgentOwnerIdCardNo(form.getAgentOwnerIdCardNo());
		view.setAgentAddress(form.getAgentAddress());
		view.setAgentPhone(form.getAgentPhone());
		view.setBrandId(form.getBrandId());
		view.setCarStyleId(form.getCarStyleId());
		view.setVendorId(form.getVendorId());

		view.setMaterialImages1(form.getMaterialImages1());
		view.setMaterialImages2(form.getMaterialImages2());
		view.setMaterialImages3(form.getMaterialImages3());
		view.setMaterialImages4(form.getMaterialImages4());
		view.setMaterialImages5(form.getMaterialImages5());
		view.setOwnersGroupPhoto(form.getOwnersGroupPhoto());

		view.setImageBsFlag("0");

		view.setCarScore(form.getCarScore());
		view.setMemberId(form.getMemberId());
		
		view.setTransferPrice(form.getTransferPrice());
		view.setTaxNo(form.getTaxNo());
		
		view.setFromType(form.getFromType());
		
		//车辆品牌
		Vendor vendor = Vendor.getInstance();
		CarStyle carStyle = CarStyle.getInstance();
		if(form.getFromType() == 1){
			view.setVendorList(vendor.findAllVendorInfo());
			view.setCarStyleList(carStyle.getAllCarStyleInfo(form.getBrandId()));
			view.setBrandList(brand.getAllVehicleBrandInfo(form.getVendorId()));	
		}else{
			view.setVendorList(commercialVehicle.findAllVendorInfo());
			view.setCarStyleList(commercialVehicle.getAllStyleInfo(form.getBrandId()));
			view.setBrandList(commercialVehicle.getAllVehicleBrandInfo(form.getVendorId()));
		}

		return view;
	}

	/**
	 * 从Entity 给viewHelper 赋值
	 * 
	 * @param viewHelper
	 * @param car
	 * @throws TmsDaoException
	 */
	private TransferRgAdvanceInputViewHelper setViewhelperFromEntity(
			TransferRgAdvanceInputViewHelper view, Car car) throws TmsDaoException {

		view.setEngine(car.getEngine());
		if (car.getColor() > 0) {
			view.setColor(String.valueOf(car.getColor()));
		} else {
			view.setColor("");
		}
		view.setRegDate(car.getRegDate());
		view.setVendorCode(car.getVendorCode());
		view.setModelCode(car.getModelCode());

		if (car.getCarUse() > 0) {
			view.setCarUse(String.valueOf(car.getCarUse()));
		} else {
			view.setCarUse("");
		}
		if (car.getCarType() > 0) {
			view.setCarType(String.valueOf(car.getCarType()));
		} else {
			view.setCarType("");
		}

		view.setSendCardDate(car.getSendCardDate());
		if (car.getCardType() > 0) {
			view.setCardType(String.valueOf(car.getCardType()));
		} else {
			view.setCardType("");
		}
		view.setRegisterCardNo(car.getRegisterCardNo());
		if (car.getCountryStyle() >= 0) {
			view.setCountryStyle(String.valueOf(car.getCountryStyle()));
		} else {
			view.setCountryStyle("");
		}

		view.setCompanyContactPerson(car.getCompanyContactPerson());
		view.setCarOwnerIdCardNo(car.getCarOwnerIdCardNo());
		view.setOwnerOrgNo(car.getOwnerOrgNo());

		view.setOwnerPhone(car.getOwnerPhone());
		view.setOwnerAddress(car.getOwnerAddress());
		view.setOwnerOrgNo(car.getOwnerOrgNo());
		view.setCarOwner(car.getCarOwner());
		view.setBrandId(car.getBrandId());
		view.setCarStyleId(car.getCarStyleId());
		view.setVendorId(car.getVendorId());

		view.setImageBsFlag("0");
		
		if(car.getVendorId()!=null && !"".equals(car.getVendorId()))
		{
			//车辆品牌
			Vendor vendor = Vendor.getInstance();
			CarStyle carStyle = CarStyle.getInstance();
			VehicleBrand brand = VehicleBrand.getInstance();
			CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
			String str = car.getVendorId().substring(0,1);
			if("S".equals(str))
			{
				view.setFromType(1);
				view.setVendorList(vendor.findAllVendorInfo());
				view.setCarStyleList(carStyle.getAllCarStyleInfo(car.getBrandId()));
				view.setBrandList(brand.getAllVehicleBrandInfo(car.getVendorId()));
			}else{
				view.setFromType(2);
				view.setVendorList(commercialVehicle.findAllVendorInfo());
				view.setCarStyleList(commercialVehicle.getAllStyleInfo(car.getBrandId()));
				view.setBrandList(commercialVehicle.getAllVehicleBrandInfo(car.getVendorId()));
			}
		}
		return view;
	}

	public String getCarName() {
		return carName;
	}

	public void setCarName(String carName) {
		this.carName = carName;
	}

}
